#pragma once
#include<assert.h>
#include<iostream>
using namespace std;

namespace jxh {
	class string {
	public:
		/*string(const char* str="")
			:_str(new char[strlen(str)+1])
			,_size(strlen(str))
			,_capacity(strlen(str))
		{
			strcpy(_str, str);
		}*/

		string(const char* str = ""){
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_size + 1];
			strcpy(_str, str);
		}
		string(const string& s) {
			string tmp(s._str);
			swap(tmp);
		}
		string& operator=(string s) {
			swap(s);
			return *this;
		}
		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin() {
			return _str;
		}
		iterator end() {
			return _str + _size;
		}
		const_iterator begin()const {
			return _str;
		}
		const_iterator end()const {
			return _str + _size;
		}

		const char* c_str()const{
			return _str;
		}
		size_t size()const {
			return _size;
		}
		char& operator[](size_t pos) {
			assert(pos <= _size);
			return _str[pos];
		}
		const char& operator[](size_t pos) const {
			assert(pos <= _size);
			return _str[pos];
		}
		void reserve(size_t n) {
			if (n > _capacity) {
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}
		void push_back(char ch) {
			if (_size == _capacity) {
				int newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}
			_str[_size] = ch;
			_size++;
			_str[_size] = '\0';
		}
		void append(const char* str) {
			size_t len = strlen(str);
			if (len + _size > _capacity) {
				reserve(len + _size);
			}
			strcpy(_str + _size, str);
			_size = len + _size;
		}
		string& operator+=(char ch) {
			push_back(ch);
			return *this;
		}
		string& operator+=(const char* str) {
			append(str);
			return *this;
		}
		void insert(size_t pos, char ch) {
			assert(pos <= _size);
			if (_size == _capacity) {
				int newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}
			for (size_t i = _size+1; i > pos; i--) {
				_str[i] = _str[i-1];
			}
			_str[pos] = ch;
			_size++;
		}
		void insert(size_t pos, const char* str) {
			assert(pos <= _size);
			size_t len = strlen(str);
			if (_size + len > _capacity) {
				reserve(_size + len);
			}
			for (size_t i = _size + len; i >= pos + len; i--) {
				_str[i] = _str[i - len];
			}
			strncpy(_str + pos, str, len);
			_size += len;
		}
		void erase(size_t pos, size_t len=npos) {
			assert(pos < _size);
			if (len == npos || pos + len - 1 >= _size - 1) {
				_str[pos] = '\0';
				_size = pos;
			}
			else {
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
		}
		void swap(string& s) {
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}
		size_t find(char ch, size_t pos = 0) {
			for (size_t i = 0; i < _size; i++) {
				if (ch == _str[i])
					return i;
			}
			return npos;
		}
		size_t find(const char* str, size_t pos = 0) {
			const char* ptr = strstr(_str + pos, str);
			if (ptr == nullptr)return npos;
			else return ptr - _str;
		}
		string substr(size_t pos, size_t len = npos) {
			assert(pos < _size);
			size_t end = len + pos;
			if (len == npos || len + pos >= _size) {
				end = _size;
			}
			string str;
			str.reserve(end - pos);
			for (size_t i = pos; i < end; i++) {
				str += _str[i];
			}
			return str;
		}
		void clear() {
			_size = 0;
			_str[0] = '\0';
		}
		void print_str() {
			for (size_t i = 0; i < _size; i++) {
				cout << _str[i] << ' ';
			}
			cout << endl;
		}

		~string() {
			delete[] _str;
			_str = nullptr;
			_size = 0;
			_capacity = 0;
		}


	private:
		char* _str;
		size_t _size;
		size_t _capacity;
		static size_t npos;
	};
	size_t string::npos = -1;
	ostream& operator<<(ostream& out, const string& s) {
		for (auto ch : s) {
			out << ch;
		}
		return out;
	}
	istream& operator>>(istream& in, string& s) {
		s.clear();
		char buff[128];
		char ch = in.get();
		int i = 0;
		while (ch != ' ' && ch != '\n') {
			buff[i++] = ch;
			if (i == 127) {
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0) {
			buff[i] = '\0';
			s += buff;
		}
		return in;
	}


	
}
